﻿using System.Windows.Input;
#if WINDOWS_PHONE
using System.Windows;
#elif NETFX_CORE
using Windows.UI.Xaml;
#endif
#if WINDOWS_PHONE || NETFX_CORE
namespace Nx.Commands
{
    public static class DoubleTap
    {
        private static readonly DependencyProperty DoubleTapCommandBehaviorProperty = DependencyProperty.RegisterAttached(
            "TapCommandBehavior",
            typeof(ControlDoubleTapCommandBehavior),
            typeof(DoubleTap),
            null);

        public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached(
            "Command",
            typeof(ICommand),
            typeof(DoubleTap),
#if WINDOWS_PHONE
            new PropertyMetadata(OnSetCommandCallback));
#elif NETFX_CORE
 new PropertyMetadata(null, OnSetCommandCallback));
#endif

        public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.RegisterAttached(
            "CommandParameter",
            typeof(object),
            typeof(DoubleTap),
#if WINDOWS_PHONE
            new PropertyMetadata(OnSetCommandParameterCallback));
#elif NETFX_CORE
 new PropertyMetadata(null, OnSetCommandParameterCallback));
#endif

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "Only works for buttonbase")]
        public static void SetCommand(FrameworkElement control, ICommand command)
        {
            if (control == null) throw new System.ArgumentNullException("control");
            control.SetValue(CommandProperty, command);
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "Only works for buttonbase")]
        public static ICommand GetCommand(FrameworkElement control)
        {
            if (control == null) throw new System.ArgumentNullException("control");
            return control.GetValue(CommandProperty) as ICommand;
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "Only works for buttonbase")]
        public static void SetCommandParameter(FrameworkElement control, object parameter)
        {
            if (control == null) throw new System.ArgumentNullException("buttonBase");
            control.SetValue(CommandParameterProperty, parameter);
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "Only works for buttonbase")]
        public static object GetCommandParameter(FrameworkElement control)
        {
            if (control == null) throw new System.ArgumentNullException("buttonBase");
            return control.GetValue(CommandParameterProperty);
        }

        private static void OnSetCommandCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            FrameworkElement control = dependencyObject as FrameworkElement;
            if (control != null)
            {
                ControlDoubleTapCommandBehavior behavior = GetOrCreateBehavior(control);
                behavior.Command = e.NewValue as ICommand;
            }
        }

        private static void OnSetCommandParameterCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            FrameworkElement control = dependencyObject as FrameworkElement;
            if (control != null)
            {
                ControlDoubleTapCommandBehavior behavior = GetOrCreateBehavior(control);
                behavior.CommandParameter = e.NewValue;
            }
        }

        private static ControlDoubleTapCommandBehavior GetOrCreateBehavior(FrameworkElement control)
        {
            ControlDoubleTapCommandBehavior behavior = control.GetValue(DoubleTapCommandBehaviorProperty) as ControlDoubleTapCommandBehavior;
            if (behavior == null)
            {
                behavior = new ControlDoubleTapCommandBehavior(control);
                control.SetValue(DoubleTapCommandBehaviorProperty, behavior);
            }

            return behavior;
        }
    }
}
#endif